<?php
//set session save path
session_save_path("../sessions");
// Initialize a session. 
session_start();
$count++;
$sessionId = session_id();
if (!$userID)
{
	echo "<br/><span style=\"color: #FF0000\">You'll need to log in to place your vote.  I would rather not have random people, people who may not even be using the pool, to skew the results.  Log in and refresh this page.</span><br/>";
	break;
}
//Connect to DB
	//set up database variables
	$dbusername = "joshhughes22";
	$dbpass = "ellencallis";
	$dbhost = "localhost";
	$database = "joshhughes_com_-_football";
	//Connect TO The Database
	mysql_connect($dbhost,$dbusername,$dbpass) or die("Cannot connect to the database.<br>" . mysql_error());
	//Open Database
	mysql_select_db($database) or die("Cannot select the database.<br>" . mysql_error());

//Check To See if user has voted.
$hasVotedSQL = "SELECT * FROM NFLPolls WHERE (poll_id=$pollID AND userID=$userID)";
$result = mysql_query($hasVotedSQL) or die("Cannot query the database.<br>" . mysql_error());
if (mysql_num_rows($result) > 0) {
	$myrow = mysql_fetch_array($result);
	echo "<br/><span style=\"color: #FF0000\">You voted for <b>".$myrow["Vote"]."</b>, (".date('l F jS, Y g:i A [T]',strtotime($myrow["VoteTime"])).").</span><br/>";
	//break;
} 
else {// If user HAS NOT voted, Insert the vote
	$SQL = "INSERT INTO NFLPolls VALUES ($pollID,$userID,'$vote','".date('Y-m-d H:i:s')."')";
	mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
}
// Setup displaying the results
echo "<h2>Result:</h2>
<table>";
// Pull Votes From DB to Display results
	// Get the total number of votes for this poll
	$numSQL = "SELECT * FROM NFLPolls WHERE poll_id=$pollID";
	$numresult = mysql_query($numSQL) or die("Cannot query the database.<br>" . mysql_error());
	$num_rows = mysql_num_rows($numresult);
	// GET Distinct Vote Options
	$SQL = "SELECT DISTINCTROW Vote FROM NFLPolls WHERE poll_id=$pollID";
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	// For each Vote option get the number of times that option has been voted.
	while($myrow = mysql_fetch_array($result))
	{
		if ($myrow["Vote"] != "" && $myrow["Vote"] != NULL) {
			$insideSQL = "SELECT Vote FROM NFLPolls WHERE poll_id=$pollID AND Vote='".$myrow["Vote"]."'";
			$insideresult = mysql_query($insideSQL) or die("Cannot query the database.<br>" . mysql_error());
			$insidenum_rows = mysql_num_rows($insideresult);
			echo "
			<tr>
			<td>".$myrow["Vote"]."</td>
			<td>($insidenum_rows)</td>
			<td>
			<img src=\"http://www.w3schools.com/php/poll.gif\" width='".(100*round($insidenum_rows/$num_rows,2))."' height='20' />".(100*round($insidenum_rows/$num_rows,2))."%
			</td>
			</tr>";
		}
	}
echo "</table>";
?>